package com.tarena.service;

import com.tarena.pojo.Message;
import com.tarena.pojo.MessageType;
import com.tarena.pojo.User;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class ChatServer {
    private ServerSocket ss;
    /*构建模拟数据，替代数据库*/
    public static HashMap<String,User> userData = new HashMap<>();
    /*静态块*/
    static{
        userData.put("root",new User("root","root",null));
        userData.put("root1",new User("root1","root1",null));
        userData.put("root2",new User("root2","root2",null));
        userData.put("root3",new User("root3","root3",null));
        userData.put("root4",new User("root4","root4",null));
        userData.put("root5",new User("root5","root5",null));
        userData.put("root6",new User("root6","root6",null));
        userData.put("root7",new User("root7","root7",null));
    }


    /*校验用户user对象的用户名和对象是否存在*/
    public boolean checkUser(String username,String paaword){
     /*将用户名作为key到hashmao里面找到对象的user对象*/
       User user= userData.get(username);
     if(user==null){/*说明没有这个用户*/
         return false;
     }if (!user.getPassword().equals(paaword)){
         return false;
        }
     return true;
    }
    public ChatServer() {
        /*启动服务器*/
        System.out.println("服务端开始启动，并监听8999端口。。。。");
        try {
            ss= new ServerSocket(9003);
            while (true){
                /*持续监听8999端口，获取和客户端通信的socket对象，假如没有客户端连接，accept方法会一直阻塞*/
                Socket socket = ss.accept();
                /*一旦连接建立，立即获得输入流，读取客户端的信息*/
                InputStream in = socket.getInputStream();
                ObjectInputStream ois = new ObjectInputStream(in);
                /*获取输出流，以备获取message对象*/
                OutputStream out =socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(out);
                /*读取客户端发送的uesr对象，进行校验*/
                User user=(User) ois.readObject();
                /*如果登录成功，返回带有成功信息的message对象，否则，返回失败的message*/
                Message message=new Message();
                /*校验用户是否登录*/
                boolean userLogined=ManageConnectServerList.isUserLogined(user);
                if(userLogined){
                    message.setMagType(MessageType.MESSAGE_USER_LOGINEND);
                    oos.writeObject(message);
                    socket.close();
                    continue;
                }
                if(checkUser(user.getUsername(),user.getPassword())){
                    /*校验成功*/
                    message.setMagType(MessageType.MESSAGE_LOGIN_SUCCEED);
                    oos.writeObject(message);
                    /*启动服务端线程类*/
                    ServerSocketThread sst = new ServerSocketThread(socket,user);
                    sst.start();
                    /*将sst添加到hashmap(静态的方法，用类名调用就可以)*/
                    ManageConnectServerList.addThreadToMap(user,sst);
                }else{
                    /*校验失败*/
                    message.setMagType(MessageType.MESSAGE_LOGIN_FAIL);
                    oos.writeObject(message);
                    socket.close();/*关闭socket*/
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
}
